#ifndef BACKUPMANAGER_H
#define BACKUPMANAGER_H

#include <QObject>
#include <QString>
#include <QVector>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <iostream>
#include <QCryptographicHash>
#include <pthread.h>

struct FileCopyArgs {
    std::string source;
    std::string target;
    off_t startOffset;
    off_t endOffset;
};

class BackupManager : public QObject {
    Q_OBJECT

public:
    BackupManager(QObject *parent = nullptr);
    void startBackup(const QString &sourceDir, const QString &targetDir, bool incremental);
    void setMaxThreads(int maxThreads); // 添加设置最大线程数的方法

signals:
    void progressUpdated(int progress);
    void finished();
    void logMessage(const QString &message);  // 新增日志消息信号

public slots:


private:
    void copyFile(const std::string &source, const std::string &target, bool useMultiThread);
    void copyDirectory(const std::string &source, const std::string &target, bool incremental);
    int calculateTotalFiles(const std::string &dirPath);
    bool createDirectoryIfNotExists(const std::string &dirPath);
    struct stat getFileStat(const std::string &filePath);
    QByteArray getFileChecksum(const std::string &filePath);
    int totalFiles;
    int copiedFiles;
    int maxThreads;
    void emitFinished();

    static void* fileCopyThread(void* arg);
};
#endif // BACKUPMANAGER_H
